﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Playkanaler.WebPageSource;
using Microsoft.MediaCenter.UI;

namespace Playkanaler.WebPageSource
{
    public class PageLoadedEventArg:EventArgs
    {
        public BasicPage Page { get; set; }
    }

    public class SettingFromWeb:Setting
    {
        public BasicPage Page { get; set; }
        public SettingFromWeb ()
	    {
            SelectedTabIndex = -1; //      
	    }

        public event EventHandler<PageLoadedEventArg> LoadingDone;
       

        public override void Load()
        {
            if (!Loaded)
            {

                //Application.DeferredInvokeOnWorkerThread(doLoading, loadingDone, Page);
                if(Page!=null)AddTabsFromPage(Page);

                Loaded = true;
            }
        }
        public void AddTabFromPage(BasicPage page,string title)
        {
            TabFromWeb tab = new TabFromWeb(title, Tabs.Count);            
            tab.Page = page;
            Tabs.Add(tab);
            if (SelectedTabIndex == -1) SelectedTabIndex = 0;
        }
        public void AddTabsFromPage(BasicPage page)
        {
            Application.DeferredInvokeOnWorkerThread(doLoading, loadingDone, page);
        }
     

        void doLoading(object pag)
        {
            BasicPage page = (BasicPage)pag;
            if (!page.PageLoaded)
            {
                page.Load();
            }
            
        }
        void loadingDone(object pag)
        {
            BasicPage page = (BasicPage)pag;
            foreach (PageLink pl in page.Tabs)
            {
                TabFromWeb tab = new TabFromWeb(pl.name, Tabs.Count);

                if (!pl.selected)
                {
                    BasicPage tabPage = (BasicPage)Activator.CreateInstance(page.GetType());
                    tabPage.Url = pl.url;
                    tab.Page = tabPage;                    
                }
                else
                {
                    tab.Page = page;
                }
                Tabs.Add(tab);
            }
            if(SelectedTabIndex==-1) SelectedTabIndex = 0;
            if (LoadingDone != null) LoadingDone(this, new PageLoadedEventArg() { Page = page });
           
        }


    }

    public class TabFromWeb : Tab
    {
        public BasicPage Page { get; set; }

        Group group = new Group();

        public TabFromWeb()
        {
            this.Add(group);
            EpView largeview = new EpView();

            View = largeview;
        }
        public TabFromWeb(string Title, int indx)
        {
            this.Title = Title;
            this.Index = indx;
            this.Add(group);
            EpView largeview = new EpView();

            View = largeview;
        }

       
        public override void Load()
        {
            if (!IsLoaded)
            {
                if (Page != null)
                {
                    if (!Page.PageLoaded)
                    {
                        Application.DeferredInvokeOnWorkerThread(doLoading, loadingDone, null);
                    }
                    else
                    {
                        loadingDone(null);
                    }
                }
                IsLoaded = true;
                
            }
        }
        void doLoading(object obj)
        {
            Page.Load();            
        }
        void loadingDone(object obj)
        {
            AddItemFromPage(Page);
        }

      
        public override void GetMore()
        {
            if (Page!=null && Page.PageLoaded && Page.HasMorePages)
            {
                Application.DeferredInvokeOnWorkerThread(doGetMore, getMoreDone,null);                       
            }

        }
        BasicPage morepage;
        void doGetMore(object obj)
        {
            morepage = Page.LoadMoreItemPage();
        }
        void getMoreDone(object obj)
        {
            AddItemFromPage(morepage);
        }

        public void AddItemFromPage(BasicPage page)
        {
            AddItemFromPageLinks(page.Items);
        }
        public void AddItemFromPageLinks(IEnumerable<PageLink> links)
        {
            foreach (PageLink pl in links)
            {
                Item item = new Item();
                item.Title = pl.name;
                item.ImageUrl = pl.imageurl;
                item.Description = pl.description;
                item.Path = pl.url;
                foreach (var attr in pl.data)
                {
                    item.Attributes[attr.Key] = attr.Value;
                }
                group.Add(item);
            }
        }

    }
}
